import 'package:flutter/material.dart';
import 'package:kasax/commons/commons.dart';

class AlertDialogDemo extends StatefulWidget {
  const AlertDialogDemo({Key? key}) : super(key: key);

  @override
  _AlertDialogDemoState createState() => _AlertDialogDemoState();
}

enum ActionType { OK, CANCEL, RETRY }

class _AlertDialogDemoState extends State<AlertDialogDemo> {
  String _selectValue = "Northing";

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Padding(
        padding: EdgeInsets.all(16),
        child: SingleChildScrollView(
          child: Container(
            child: Column(
              children: [
                Container(
                  child: Text(
                    "您选择了：$_selectValue",
                    style: TextStyle(fontSize: 18),
                  ),
                ),
                SizedBox(
                  height: 15,
                ),
                ListView(shrinkWrap: true, children: [
                  buildCircleButton("显示弹窗", () {
                    showAlertDialog();
                  })
                ])
              ],
            ),
          ),
        ),
      ),
    );
  }

  Future showAlertDialog() async {
    final option = await showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            titlePadding: EdgeInsets.all(10),
            contentPadding: EdgeInsets.all(10),
            title: Text(
              "定位失败",
              textAlign: TextAlign.center,
            ),
            content: Text("请确认您的定位服务已授权,并且处于开启中"),
            actions: [
              FlatButton(
                  onPressed: () {
                    Navigator.pop(context, ActionType.RETRY);
                  },
                  child: Text("重试")),
              FlatButton(
                  onPressed: () {
                    Navigator.pop(context, ActionType.CANCEL);
                  },
                  child: Text("取消")),
            ],
          );
        });
    String value = _selectValue;
    switch (option) {
      case ActionType.OK:
        value = "下一步";
        break;
      case ActionType.RETRY:
        value = "重新执行";
        break;
      case ActionType.CANCEL:
        value = "取消操作";
        break;
    }
    setState(() {
      _selectValue = value;
    });
  }
}
